package com.polidea.rxohosble2.sample.example1a_background_scanning;

import com.polidea.rxohosble2.exceptions.BleScanException;
import com.polidea.rxohosble2.internal.RxBleLog;
import com.polidea.rxohosble2.sample.SampleApplication;
import com.polidea.rxohosble2.scan.BackgroundScanner;
import com.polidea.rxohosble2.scan.ScanResult;
import ohos.aafwk.content.Intent;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;

import java.util.List;

public class  ScanReceiver extends CommonEventSubscriber {
    public ScanReceiver(CommonEventSubscribeInfo subscribeInfo) {
        super(subscribeInfo);
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        RxBleLog.i("onReceiveEvent commonEventData:"+commonEventData);
        Intent intent = commonEventData.getIntent();
        BackgroundScanner backgroundScanner = SampleApplication.getRxBleClient(null).getBackgroundScanner();

        try {
            final List<ScanResult> scanResults = backgroundScanner.onScanResultReceived(intent);
            RxBleLog.i("ScanReceiver " + "Scan results received: " + scanResults);
        } catch (BleScanException exception) {
            RxBleLog.w("ScanReceiver " + "Failed to scan devices"+ exception);
        }
    }
}
